package com.lv.fast.DTO;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.lv.fast.DO.SysMenuDO;
import com.lv.fast.annotation.EnumCheck;
import com.lv.fast.constant.JsonConstant;
import com.lv.fast.enums.SysMenuTypeEnum;
import com.lv.fast.model.ExtendBean;
import com.lv.fast.valid.group.AddStrategy;
import com.lv.fast.valid.group.UpdateStrategy;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.SneakyThrows;
import org.hibernate.validator.constraints.Length;

import javax.validation.constraints.*;
import java.util.Set;

/**
 * @Description
 * @Author jie.lv
 */
@Data
@ApiModel("菜单对象")
public class SysMenuDTO extends ExtendBean {

    @ApiModelProperty(value = "菜单名称",example = "角色管理")
    @NotBlank(message = "菜单名称不能为空", groups = {AddStrategy.class})
    @Length(min = 2, max = 20, message = "菜单名称长度2-20哥字符")
    private String name;

    @ApiModelProperty(value = "菜单描述",example = "角色管理菜单页面")
    @NotBlank(message = "菜单描述不能为空", groups = {AddStrategy.class})
    @Length(min = 2, max = 50, message = "菜单描述长度2-50哥字符")
    private String menuDescribe;

    @ApiModelProperty(value = "菜单前台路由地址",example = "/sys/roleManage")
    @Length(min = 1, max = 50, message = "菜单描述长度2-50哥字符")
    private String url;

    @ApiModelProperty(value = "菜单类型  1目录  2菜单  3按钮",example = "2")
    @NotBlank(message = "菜单类型不能为空", groups = {AddStrategy.class})
    @Null(message = "不能修改菜单类型", groups = {UpdateStrategy.class})
    @EnumCheck(enumClass = SysMenuTypeEnum.class, message = "菜单类型非法")
    private String type;

    @ApiModelProperty(value = "菜单icon",example = "role-manage")
    @Length(min = 1, max = 20, message = "菜单描述长度2-50哥字符")
    private String icon;

    @ApiModelProperty(value = "菜单排序  0-100整数",example = "1")
    @NotNull(message = "菜单排序不能为空", groups = {AddStrategy.class})
    @Min(value = 0L, message = "排序不能为负数")
    @Max(value = 100L, message = "排序最大值100")
    private Integer orderNum;

    @ApiModelProperty(value = "授权标识集合",example = "[\"sys:role:update\",\"sys:role:add\"]")
    private Set<String> perms;

    @ApiModelProperty(value = "菜单上级Id",example = "0")
    @NotBlank(message = "菜单上级Id不能为空", groups = {AddStrategy.class})
    private String parentId;

    @SneakyThrows
    public SysMenuDO toDO(){
        SysMenuDO sysMenuDO = BeanUtil.copyProperties(this, SysMenuDO.class, "perms");
        Set<String> perms = this.perms;
        if (CollectionUtil.isNotEmpty(perms)){
            sysMenuDO.setPerms(JsonConstant.objectMapper.writeValueAsString(perms));
        }
        return sysMenuDO;
    }
}
